package com.woldier.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woldier.common.R;
import com.woldier.dto.DishDto;
import com.woldier.entity.Dish;
import com.woldier.entity.DishFlavor;
import com.woldier.service.DishFlavorService;
import com.woldier.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 菜品
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public R save(@RequestBody DishDto dishDto){
        /*保存*/
        dishService.saveWithFlavor(dishDto);

        return R.success("保存成功");
    }


    /**
     * 分页数据
     * @param page
     * @param pageSize
     * @param name 菜名
     */
    @GetMapping("page")
    public R getPage(int page , int pageSize,String name){


        /*分页查询*/



        /*更具分类id查询到对应的名称*/
        Page<DishDto> page1 = dishService.getByPage(page, pageSize, name);

        return R.success(page1);
    }


    /**
     * http://localhost/dish/1397849739276890114
     * 根据请求id返回对应菜品信息 这里必须要用dto类型
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getById(@PathVariable("id") Long id){

        /*更具id查询dish*/
        Dish dish = dishService.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        /*更具dish_id查询dish_flavor表查询口味*/
        /*查询数据库*/
        List<DishFlavor> list = dishFlavorService.getByDishId(dishDto.getId());
        /*将dishFlavor list插入dto*/
        dishDto.setFlavors(list);
        return R.success(dishDto);
    }


    /**
     * 菜品信息的修改
     * @param dishDto
     * @return
     */

    @PutMapping
    public  R update(@RequestBody DishDto dishDto){

        dishService.updateWithFlavor(dishDto);

        return R.success("成功");
    }

    /**
     * 修改状态
     * @param ids
     * @param status
     * @return
     */

    @PostMapping("status/{status}")
    public R updateStatus(Long[] ids,@PathVariable(value = "status",required = false) Integer status){
        /*将id与状态封装到dish实体中 便于更新*/
        List<Dish> list = getDishList(ids, status);
        dishService.updateBatchById(list);
        return R.success("成功");

    }

    /**
     * 根据id删除
     * @param ids
     * @return
     */

    @DeleteMapping
    public R delete(Long[] ids){
        List<Dish> list = getDishList(ids);
        boolean b = dishService.updateBatchById(list);
        return b?R.success("成功"):R.error("失败");
    }


    /**
     * 通过
     * @return
     */
    @GetMapping("list")
    public R getListById(Long categoryId){
        List<DishDto> dishDtos = null;
        /*首先从redis中取*/
        dishDtos = (List<DishDto>) redisTemplate.opsForValue().get("dish_" + categoryId);
        if(dishDtos!=null){
            /*不为空说明有数据直接返回*/
            return R.success(dishDtos);
        }

        /*条件构造器*/
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,categoryId);
        /*状态必须为1*/
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        /*进行查询*/
        List<Dish> list = dishService.list(lambdaQueryWrapper);
        /*查询dish对应的口味 封装为dishDto*/
        dishDtos = new ArrayList<>();
        /*便利dish*/
        for (Dish dish : list) {
            DishDto dishDto = new DishDto();
            /*对象拷贝*/
            BeanUtils.copyProperties(dish,dishDto);
            /*查询口味*/
            List<DishFlavor> dishFlavors = dishFlavorService.getByDishId(dishDto.getId());
            /*设置*/
            dishDto.setFlavors(dishFlavors);
            /*添加*/
            dishDtos.add(dishDto);
        }

        /*将数据加入缓存中*/
        redisTemplate.opsForValue().setIfAbsent("dish_"+categoryId,dishDtos,60, TimeUnit.MINUTES);

        return R.success(dishDtos);
    }















    /*----------------------------------------------------------------------------------------------------------------*/

    /**
     * 将id数组封装到dish中
     * @param ids
     * @return
     */
    private List<Dish> getDishList(Long[] ids){
        /*将id与状态封装到dish实体中 便于删除*/
        List<Dish> list= new ArrayList<>();

        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            list.add(dish);
        }
        return list;
    }

    /**
     * 将id数组以及状态封装到dish中
     * @param ids
     * @return
     */
    private List<Dish> getDishList(Long[] ids,int status){
        /*将id与状态封装到dish实体中 便于删除*/
        List<Dish> list= new ArrayList<>();

        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            list.add(dish);
        }
        return list;
    }


}
